<?php

/**
 * @author HoangLuong - VCCorp
 * @copyright 2013
 */

class content extends MX_Controller
{
    
    public function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_Layout');
        $this->load->library('Minifier');
        $this->load->library('Myvietstring');
        $this->load->library('Myimage');
		$this->load->model("content/content_admin_model");	      
    }
    
    public function edit($id = ""){
  		$this->mycommon->authAdmin();
        $this->minifier->AddJsNoPack('jquery.validate');
        $this->minifier->AddJsNoPack('tiny_mce/tiny_mce');
        $this->admin_layout->_loadHeader("");
        $this->admin_layout->_loadLeftMenu();
        
        $data = array();
       	$data['error'] = "";
       	$data['success'] = ""; 
        $data['content'] = $this->content_admin_model->get_content_by_id($id);                  
     if($this->input->post('Submit')){
        $input = $this->input->post();
 				$filename = $_FILES['upload']['name'];
                $targetFile = $this->input->post('hidImg');
            	$url_image = "";
            	if($filename != ""){
            		$type = $_FILES['upload']['type'];
            		if( $type == 'image/jpeg' || $type == 'image/png' || $type == 'image/pjpeg' || $type == 'image/gif' && $_FILES['upload']['size'] < (1024 * 3)){             
	            		$pos = strrpos($filename, '.');
				        $name = substr($filename, 0, $pos);
				        $name = strtolower(str_replace(' ', '-', $this->myvietstring->removeVietChars($name)));
				        $name = $this->myvietstring->removeInvalidCharacter($name);
				        $name = strlen(strlen($name) > 10) ? substr($name, 0, 10) : $name;
				        $name.= '-' . time();
				        $name_thumb = 'thumb_' . $name;
				        $fileParts = pathinfo($_FILES['upload']['name']);
				        $targetFile = $name .".".$fileParts['extension'];
				        $this->myimage->resizeFromWidthAndHeight($_FILES['upload']['tmp_name'], 200, 200, "uploads/images/" . $name_thumb.'.'.$fileParts['extension']);			            	
	            		$check = move_uploaded_file($_FILES['upload']['tmp_name'],"uploads/images/" . $targetFile);            			            		
	            		$url_image = "uploads/images/".$name_thumb.'.'.$fileParts['extension'];
	            		if(!$check)
	            			$data['error'] = "Upload không thành công!";	            			
			        }
			        else{
			        	$data['error'] = "Định dạng của file không được hỗ trợ!";
			        }            			
            	}
        $id = $input['id'];
        unset($input['frmToken'],$input['Submit'],$input['id'],$input['upload'],$input['hidImg']);
        if($targetFile){
            $input = array_merge($input,array('image'=>$targetFile));
        }
      //  debug($input);
        $this->content_admin_model->update_content($id,$input);
     //   redirect(site_url("admin/content/edit/".$id));
     }
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken',$data['frmToken']);
        $this->load->view("content/content_edit_view",$data);
        $this->admin_layout->_loadFooter();
    }
}

?>